import java.util.HashMap;
import java.util.TreeMap;
/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-07-11
 * Time: 22:07
 */
//演示hashmap和treemap的区别和简单应用
public class ShowMap {
    //自定义类型
    public static class Node {
        public int value;
        
        public Node(int v) {
            value = v;
        }
    }
    
    // hashmap是一个无序的(K V)表，并且是按值传递的
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("dog", "这是狗");
        System.out.println(map.containsKey("dog"));
        System.out.println(map.containsKey("do"));
        System.out.println(map.get("dog"));
        
        //put不仅是放初始值，也是更新
        map.put("dog", "我被更新了");
        System.out.println(map.get("dog"));

//		map.remove("dog");
//		System.out.println(map.containsKey("dog"));
//		System.out.println(map.get("dog"));
        
        //hashmap对于java中已有的类型，查询的是值传递的内容，不管有没有对象引用该值，
        String test1 = "dog";
        String test2 = "dog";
        //都能查到
        System.out.println(map.containsKey(test1));
        System.out.println(map.containsKey(test2));
        
        HashMap<Integer, String> map2 = new HashMap<>();
        map2.put(1234567, "我是1234567");
        
        Integer a = 1234567;
        Integer b = 1234567;
        
        System.out.println(a == b);
        System.out.println(map2.containsKey(a));
        System.out.println(map2.containsKey(b));
        
        Node node1 = new Node(1);
        Node node2 = new Node(1);
        HashMap<Node, String> map3 = new HashMap<>();
        map3.put(node1, "我进来了！");
        //对于自定义的类型，传的是引用类型
        System.out.println(map3.containsKey(node1));
        System.out.println(map3.containsKey(node2));//没有放入该引用
        
        System.out.println("===================");
        
        //顺序表，顺序存放
        TreeMap<Integer, String> treeMap1 = new TreeMap<>();
        treeMap1.put(3, "我是3");
        treeMap1.put(0, "我是3");
        treeMap1.put(7, "我是3");
        treeMap1.put(2, "我是3");
        treeMap1.put(5, "我是3");
        treeMap1.put(9, "我是3");
        
        System.out.println(treeMap1.containsKey(7));
        System.out.println(treeMap1.containsKey(6));
        System.out.println(treeMap1.get(3));
        
        treeMap1.put(3, "他是3");
        System.out.println(treeMap1.get(3));
        
        treeMap1.remove(3);
        System.out.println(treeMap1.get(3));
        
        //与hashmap的区别
        //可以查第一个值和最后一个值
        System.out.println(treeMap1.firstKey());
        System.out.println(treeMap1.lastKey());
        // <=5 离5最近的key告诉我
        System.out.println(treeMap1.floorKey(5));
        // <=6 离6最近的key告诉我
        System.out.println(treeMap1.floorKey(6));
        // >=5 离5最近的key告诉我
        System.out.println(treeMap1.ceilingKey(5));
        // >=6 离6最近的key告诉我
        System.out.println(treeMap1.ceilingKey(6));

        //这是自定义的类型
		Node node3 = new Node(3);
		Node node4 = new Node(4);
        //对于自定义的类型，放入顺序表的话，必须是可以比较的类型，需要实现比较规则，否则不能放进去
		TreeMap<Node, String> treeMap2 = new TreeMap<>();
		treeMap2.put(node3, "我是node3");
		treeMap2.put(node4, "我是node4");
    
    }
    
}
